package com.pie.drawing.components.common.utils;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.map.MapUtil;

public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public static final String CODE = "code";

    public static final String MSG = "msg";

    public R() {
        put(CODE, 0);
        put(MSG, "success");
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put(CODE, code);
        r.put(MSG, msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put(MSG, msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public boolean isOk() {
        Integer code = MapUtil.getInt(this, CODE);
        return code == 0;
    }

    public String getMsg() {
        return MapUtil.getStr(this, MSG);
    }

    /**
     * 获取出去 code msg 的数据
     *
     * @return
     */
    public Map<String, Object> getData() {
        this.remove(CODE);
        this.remove(MSG);
        return this;
    }

}